package com.longjianghuasheng.logistics

import androidx.multidex.MultiDex
import cat.ereza.customactivityoncrash.config.CaocConfig
import com.blankj.utilcode.util.Utils
import com.longjianghuasheng.logistics.business.main.WelcomeActivity
import com.longjianghuasheng.logistics.error.ErrorActivity
import com.longjianghuasheng.logistics.event.AppViewModel
import com.longjianghuasheng.logistics.event.EventViewModel
import com.tencent.mmkv.MMKV
import com.xuexiang.xui.XUI
import com.xuexiang.xui.widget.dialog.DialogLoader
import com.xuexiang.xui.widget.dialog.strategy.impl.AlertDialogStrategy
import me.hgj.jetpackmvvm.base.BaseApp

//Application全局的ViewModel，里面存放了一些账户信息，基本配置信息等
val appViewModel: AppViewModel by lazy { LongJiangHuaShengApp.appViewModelInstance }

//Application全局的ViewModel，用于发送全局通知操作
val eventViewModel: EventViewModel by lazy { LongJiangHuaShengApp.eventViewModelInstance }

class LongJiangHuaShengApp : BaseApp() {

    override fun onCreate() {
        super.onCreate()
        instance = this
        init()
    }

    private fun init() {
        XUI.init(this)
        XUI.debug(false)
//        with(AutoSizeConfig.getInstance()) {
//            isCustomFragment = true
//            unitsManager.setSupportScreenSizeDP(true)
//        }

        DialogLoader.getInstance().setIDialogStrategy(AlertDialogStrategy())

        Utils.init(instance)

        MMKV.initialize(this.filesDir.absolutePath + "/mmkv")
        eventViewModelInstance = getAppViewModelProvider().get(EventViewModel::class.java)
        appViewModelInstance = getAppViewModelProvider().get(AppViewModel::class.java)

        MultiDex.install(this)


        //防止项目崩溃，崩溃后打开错误界面
        CaocConfig.Builder.create()
            .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //default: CaocConfig.BACKGROUND_MODE_SHOW_CUSTOM
            .enabled(true)//是否启用CustomActivityOnCrash崩溃拦截机制 必须启用！不然集成这个库干啥？？？
            .showErrorDetails(false) //是否必须显示包含错误详细信息的按钮 default: true
            .showRestartButton(false) //是否必须显示“重新启动应用程序”按钮或“关闭应用程序”按钮default: true
            .logErrorOnRestart(false) //是否必须重新堆栈堆栈跟踪 default: true
            .trackActivities(true) //是否必须跟踪用户访问的活动及其生命周期调用 default: false
            .minTimeBetweenCrashesMs(2000) //应用程序崩溃之间必须经过的时间 default: 3000
            .restartActivity(WelcomeActivity::class.java) // 重启的activity
            .errorActivity(ErrorActivity::class.java) //发生错误跳转的activity
            .apply()
    }


    companion object {

        lateinit var instance: LongJiangHuaShengApp
        lateinit var eventViewModelInstance: EventViewModel
        lateinit var appViewModelInstance: AppViewModel
    }
}